﻿using AutoMapper;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleInfoController : ControllerBase
    {
        private readonly IRoleInfoService svc;

        public RoleInfoController(IRoleInfoService svc)
        {
            this.svc = svc;
        }

        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Query(o => o.Id == id).FirstOrDefault();

            return new Result() { code = 1, msg = "成功", data = info };
        }
        [HttpGet]
        public Result<IEnumerable<Base_RoleInfo>> Get()
        {
            var data = svc.Query(o=>true);

            return new Result<IEnumerable<Base_RoleInfo>>() { code = 1, msg = "成功", data = data };
        }
        [HttpGet]
        [Route("Query")]
        public PagedResult<Base_RoleInfo> Query(string? RoleName, int PageSize = 10, int PageIndex = 1)
        {
            var query = svc.Query(o => true);
            if (!string.IsNullOrEmpty(RoleName))
            {
                query = query.Where(o => o.RoleName.Contains(RoleName));
            }
            var total = query.Count();
            var list = query.OrderByDescending(o => o.Id).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
            return new PagedResult<Base_RoleInfo>() { code = 1, msg = "成功", total = total, data = list };
        }
        [HttpGet]
        [Route("QueryStatus")]
        public PagedResult<Base_RoleInfo> QueryStatus(string? RoleName, int PageSize = 10, int PageIndex = 1)
        {
            var query = svc.Query(o => true && o.Status == 1);
            if (!string.IsNullOrEmpty(RoleName))
            {
                query = query.Where(o => o.RoleName.Contains(RoleName));
            }
            var total = query.Count();
            var list = query.OrderByDescending(o => o.Id).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
            return new PagedResult<Base_RoleInfo>() { code = 1, msg = "成功", total = total, data = list };
        }
        [HttpPost]
        public Result Post(Base_RoleInfo entity)
        {
            //验证合法性

            var info = new Base_RoleInfo()
            {
                RoleName = entity.RoleName,
                RoleValue = entity.RoleValue,
                Remark = entity.Remark,
                CreateTime = DateTime.Now,
                Status = 1
            };

            var res = svc.Add(info);
            if (res)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }
        }
        [HttpPut]
        public Result Update(Base_RoleInfo info)
        {
            var data = svc.Get(o => o.Id.Equals(info.Id));
            if (data == null)
            {
                return new Result() { code = -1, msg = "查无此人" };
            }
            data.RoleName = info.RoleName;
            data.RoleValue = info.RoleValue;
            data.Remark = info.Remark;
            data.Status = info.Status;
            var list = svc.Update(data);
            if (list != null)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }
        }
        [HttpDelete]
        public Result Delete(int Id)
        {
            var data = svc.Get(o => o.Id == Id);
            if (data != null)
            {
                data.Status = 0;
                //逻辑删除
                //var entity = _mapper.Map<Base_UserInfo>(data);
                var res = svc.Update(data);
                //物理删除
                //bll.Delete(info);
                if (res)
                {
                    return new Result() { code = 1, msg = "成功" };
                }
                else
                {
                    return new Result() { code = -1, msg = "删除失败" };
                }
            }
            else
            {
                return new Result() { code = -1, msg = "你要删除的数据不存在" };
            }
        }
    }
}
